﻿using DAL;
using DAL.DBManagers;
using DAL.Models;
using System;
using System.Linq;

public partial class FlagEdit : System.Web.UI.Page
{
    Guid RecordID = Guid.Empty;
    Guid FlagID = Guid.Empty;
    FlagsManager manager = new FlagsManager();

    protected void Page_Load(object sender, EventArgs e)
    {
        lblError.Text = String.Empty;

        if (Guid.TryParse(Request.QueryString["id"], out RecordID))
        {
            if (!IsPostBack)
            {
                try
                {
                    var flag = manager.Single(s => s.RecordID == RecordID);

                    txtName.Text = flag.Name;
                    ddlNavigationMode.SelectedValue = flag.NavigationModeID.ToString();
                    txtBeginDate.DateValue = flag.BeginDate;
                }
                catch (Exception ex)
                {
                    lblError.Text = ex.Message;
                }
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            var CurrentUser = Session["User"] as UserHelper;

            Guid NewFlagID = Guid.Empty;

            if (RecordID == Guid.Empty)
            {
                if (Guid.TryParse(Request.QueryString["sid"], out FlagID))
                {
                    if (manager.Find(f => f.IsActive && f.FlagID == FlagID && f.BeginDate == txtBeginDate.DateValue.Value).Any())
                    {
                        throw new Exception("Flag already exists with this date");
                    }

                    manager.Add(new Flag
                    {
                        RecordID = Guid.NewGuid(),
                        FlagID = FlagID,
                        Name = txtName.Text,
                        NavigationModeID = Guid.Parse(ddlNavigationMode.SelectedValue),
                        BeginDate = txtBeginDate.DateValue.Value,
                        IsActive = true,
                        CreateDate = DateTime.Now,
                        CreatedBy = CurrentUser.UserID
                    });

                    NewFlagID = FlagID;
                }
                else
                {
                    var flag = new Flag
                    {
                        RecordID = Guid.NewGuid(),
                        FlagID = Guid.NewGuid(),
                        Name = txtName.Text,
                        NavigationModeID = Guid.Parse(ddlNavigationMode.SelectedValue),
                        BeginDate = txtBeginDate.DateValue.Value,
                        IsActive = true,
                        CreateDate = DateTime.Now,
                        CreatedBy = CurrentUser.UserID
                    };

                    manager.Add(flag);

                    NewFlagID = flag.FlagID;
                }
            }
            else
            {
                var flag = manager.Single(s => s.RecordID == RecordID);

                if (manager.Find(f => f.IsActive && f.FlagID == flag.FlagID && f.RecordID != RecordID && f.BeginDate == txtBeginDate.DateValue.Value).Any())
                {
                    throw new Exception("Flag already exists with this date");
                }

                flag.Name = txtName.Text;
                flag.NavigationModeID = Guid.Parse(ddlNavigationMode.SelectedValue);
                flag.BeginDate = txtBeginDate.DateValue.Value;
                flag.LastModifyDate = DateTime.Now;
                flag.LastModifier = CurrentUser.UserID;

                NewFlagID = flag.FlagID;
            }

            manager.SaveChanges();
            manager.RepiarDates(NewFlagID);

            if (FlagID == Guid.Empty && RecordID == Guid.Empty)
            {
                Response.Redirect("flagview.aspx?m=liFlags");
            }
            else
            {
                Response.Redirect(String.Format("~/flaghistory.aspx?id={0}&m={1}", NewFlagID, "liFlags"));
            }
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
        }
    }
}